package com.xbongbong.paas.service.impl;

import com.xbongbong.paas.pojo.dto.PlayBackSwitchGetDTO;
import com.xbongbong.paas.pojo.dto.PlayBackSwitchSetDTO;
import com.xbongbong.paas.pojo.vo.PlayBackSwitchVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.service.PlayBackSwitchService;
import com.xbongbong.pro.config.ProBaseConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Set;

/**
 * @author 李少杰
 * @version v1.0.0
 * @date 2020/9/11 9:13
 * @since v1.0.0
 */
@Service
public class PlayBackSwitchServiceImpl implements PlayBackSwitchService {

    private  final Logger log =  LoggerFactory.getLogger(this.getClass());

    /**
     * 开关开启状态
     */
    public static final String PLAY_BACK_SWITCH_ON = "on";

    /**
     * 开关关闭状态
     */
    public static final String PLAY_BACK_SWITCH_OFF = "off";


    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 设置流量回放开关状态
     * @param playBackSwitchSetDTO dto
     * @return vo
     */
    @Override
    public PlayBackSwitchVO setSwitchStatus(PlayBackSwitchSetDTO playBackSwitchSetDTO) {
        PlayBackSwitchVO playBackSwitchVO = new PlayBackSwitchVO();
        try {
            String token = playBackSwitchSetDTO.getToken();
            Integer time = playBackSwitchSetDTO.getTime();
            String switchStatus = playBackSwitchSetDTO.getSwitchStatus();
            Set<String> whiteCorpSet = playBackSwitchSetDTO.getWhiteCorpSet();
            if (token.equals(ProBaseConfig.getXbbPlayBackToken())) {
                switch (switchStatus) {
                    case PLAY_BACK_SWITCH_ON :
                        paasRedisHelper.setValue(RedisPrefixConstant.PLAY_BACK_SWITCH, RedisPrefixConstant.PLAY_BACK_SWITCH, PLAY_BACK_SWITCH_ON, time);
                        paasRedisHelper.addValue4Set(RedisPrefixConstant.PLAY_BACK_WHITE_CORP_SET, RedisPrefixConstant.PLAY_BACK_WHITE_CORP_SET, whiteCorpSet, time);
                        log.info("流量回放开启！ 开启时间： " + time + " s");
                        playBackSwitchVO.setSwitchStatus(switchStatus);
                        playBackSwitchVO.setWhiteCorpSet(whiteCorpSet);
                        playBackSwitchVO.setTime(Long.parseLong(playBackSwitchSetDTO.getTime().toString()));
                        playBackSwitchVO.setMsg("流量回放开启成功！");
                        break;
                    case PLAY_BACK_SWITCH_OFF:
                        paasRedisHelper.expire(RedisPrefixConstant.PLAY_BACK_SWITCH, RedisPrefixConstant.PLAY_BACK_SWITCH, 1);
                        paasRedisHelper.expire(RedisPrefixConstant.PLAY_BACK_WHITE_CORP_SET, RedisPrefixConstant.PLAY_BACK_WHITE_CORP_SET, 1);
                        log.info("流量回放关闭！");
                        playBackSwitchVO.setMsg("流量回放关闭成功！");
                        break;
                    default:
                        playBackSwitchVO.setMsg("请输入合法的开关状态！");
                        break;
                }
            } else {
                playBackSwitchVO.setMsg("安全校验失败！");
            }
        } catch (Exception e) {
            log.error("PlayBackSwitchServiceImpl 流量回放开关状态更改失败！", e);
            playBackSwitchVO.setMsg("开关状态更改失败！");
        }
        return playBackSwitchVO;
    }

    /**
     * 获取流量回放开关状态
     * @param playBackSwitchGetDTO dto
     * @return vo
     */
    @Override
    public PlayBackSwitchVO getSwitchStatus(PlayBackSwitchGetDTO playBackSwitchGetDTO) {

        PlayBackSwitchVO playBackSwitchVO = new PlayBackSwitchVO();
        try {
            String token = playBackSwitchGetDTO.getToken();
            if (token.equals(ProBaseConfig.getXbbPlayBackToken())) {
                String switchStatus = paasRedisHelper.getValue(RedisPrefixConstant.PLAY_BACK_SWITCH, RedisPrefixConstant.PLAY_BACK_SWITCH);
                Set<String> whiteCorpSet = paasRedisHelper.getValue4Set2(RedisPrefixConstant.PLAY_BACK_WHITE_CORP_SET, RedisPrefixConstant.PLAY_BACK_WHITE_CORP_SET);
                if (Objects.isNull(switchStatus) || Objects.isNull(whiteCorpSet) || (! switchStatus.equals(PLAY_BACK_SWITCH_ON))) {
                    playBackSwitchVO.setSwitchStatus(PLAY_BACK_SWITCH_OFF);
                } else {
                    playBackSwitchVO.setSwitchStatus(switchStatus);
                    playBackSwitchVO.setWhiteCorpSet(whiteCorpSet);
                    playBackSwitchVO.setTime(paasRedisHelper.getExpire(RedisPrefixConstant.PLAY_BACK_SWITCH, RedisPrefixConstant.PLAY_BACK_SWITCH));
                }
                playBackSwitchVO.setMsg("获取状态成功！");
            } else {
                playBackSwitchVO.setMsg("权限校验失败");
            }

        } catch (Exception e) {
            log.error("PlayBackSwitchServiceImpl 流量回放获取开关状态失败！", e);
            playBackSwitchVO.setMsg("获取开关状态失败！");
        }
        return playBackSwitchVO;
    }
}
